package com.hyt.it.ogt.kq.service.bm.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.service.bm.model.entity.Project;
import com.hyt.it.ogt.kq.service.bm.model.entity.ProjectSubject;
import com.hyt.it.ogt.kq.service.bm.model.entity.StudentProject;
import com.hyt.it.ogt.kq.service.bm.model.vo.*;
import com.hyt.it.ogt.kq.service.bm.service.*;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.model.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author Administrator
 *
 */
@RestController
@Api(tags = "12.对外接口Api", value = "对外接口Api")
@ApiSort(value = 12)
@RequestMapping("/bm/student")
public class StudentApi extends ApiBaseController {

	@Autowired
	IStudentInfoService iStudentInfoService;
	@Autowired
	IProjectService iProjectService;
	@Autowired
	IStudentProjectService iStudentProjectService;
	@Autowired
	IProjectSubjectService iProjectSubjectService;
	@Autowired
	IStudentSubjectService iStudentSubjectService;

	@ApiOperation(value = "12.1.1 查询考生列表", notes = "14001:成功")
	@ApiOperationSort(value = 1)
	@RequestMapping(value = "/queryPage", method = RequestMethod.GET)
	public Object queryPage(String pageJson, String studentParanJson) {
		PageParam<StudentVo> param = JSON.parseObject(pageJson, PageParam.class);
		IPage<StudentVo> page = param;
		StudentParam studentParam = JSON.parseObject(studentParanJson, StudentParam.class);
		try {
			page.setTotal(iStudentInfoService.queryStudentListCount(studentParam,new ArrayList<String>()));
			page.setRecords(iStudentInfoService.
					queryStudentList(studentParam,new ArrayList<String>(), page.getCurrent(), page.getSize()));
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(page).build();
		} catch (Exception e) {
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_15010).data(page).build();
		}
	}

	@ApiOperation(value = "12.1.3 根据项目id查询科目", notes = "14001:成功")
	@ApiOperationSort(value = 3)
	@RequestMapping(value = "/querySubject", method = RequestMethod.GET)
	public List<ProjectSubject> querySubject(String projectId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("project_id", projectId);
		List<ProjectSubject> list = iProjectSubjectService.listByMap(map);
		return list;
	}

	@ApiOperation(value = "12.1.4 获取科目信息(通过项目id)", notes = "14001:成功")
	@ApiOperationSort(value = 4)
	@RequestMapping(value = "/getSubjects", method = RequestMethod.GET)
	public Object getSubjects(String enrollId,String subjectId) {
		List<Map<String, String>> list = iProjectSubjectService.getSubjectList(enrollId,subjectId);
		return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(list).build();
	}

	@ApiOperation(value = "12.1.5 根据科目id获取考生信息列表", notes = "14001:成功")
	@ApiOperationSort(value = 5)
	@RequestMapping(value = "/getProjectCollectInfos", method = RequestMethod.GET)
	public Object getProjectCollectInfos(String enrollId, String subjectId, String candidateIds, Boolean isAll,
			String kwFlag) {
		List<String> listSList = JSONObject.parseObject(candidateIds, List.class);
		List<Map<String, Object>> list = iProjectSubjectService.
				getProjectCollectInfos(enrollId, subjectId, listSList,
				isAll, kwFlag);
		return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(list).build();
	}

	@ApiOperation(value = "12.1.6 记录成功导入的考生", notes = "14001:成功")
	@ApiOperationSort(value = 6)
	@RequestMapping(value = "/recordSuccessCandidates", method = RequestMethod.GET)
	public Object recordSuccessCandidates(String enrollId, String subjectId, String studentIds) {
		List<String> studentId = JSONObject.parseObject(studentIds, List.class);
		if (studentId != null && studentId.size() > 0) {
			iProjectSubjectService.recordSuccessCandidates(enrollId, subjectId, studentId);
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data("导入成功！").build();
		} else {
			StudentParam studentParam = new StudentParam();
			studentParam.setSubjectId(subjectId);
			studentParam.setProjectId(enrollId);
			Project project = iProjectService.getById(enrollId);
			if(project != null && project.getIsVerify() == 1) {
				studentParam.setArrageState("1");
			}
			ProjectSubject projectSubject = iProjectSubjectService.queryBySubjectId(enrollId, subjectId);
			if (projectSubject.getIsPay() == 1) {
				studentParam.setPayState("1");
			}
			try {
				List<StudentVo> studentList = iStudentInfoService.
						queryStudentList(studentParam, new ArrayList<String>(),0L, 0L);
				if (studentList != null && studentList.size() > 0) {
					iProjectSubjectService.updateSubject(enrollId, subjectId);
					return ApiResponse.
							builder().
							code(Constant.RESPONSE_CODE_SUCCESS).
							data("导入成功！").build();
				} else {
					return ApiResponse.
							builder().
							code(Constant.RESPONSE_CODE_14057).
							data("未查找到有审核通过的考生！").build();
				}
			} catch (Exception e) {
				return ApiResponse.builder().code(Constant.RESPONSE_CODE_14058).data("导入失败").build();
			}

		}
	}

	@ApiOperation(value = "12.1.7  获取报名明细", notes = "14001:成功")
	@ApiOperationSort(value = 7)
	@RequestMapping(value = "/getBmDetails", method = RequestMethod.GET)
	public Object getBmDetails(String pageJson, String enrollId, String subjectId, String name) {
		PageParam<StudentInfoDeatailVo> param = JSON.parseObject(pageJson, PageParam.class);
		IPage<StudentInfoDeatailVo> page = param;
		String payState = "";
		String auditState = "";
		try {
			Project project = iProjectService.getById(enrollId);
			if(project != null && project.getIsVerify() == 1) {
				auditState = "1";
			}
			ProjectSubject projectSubject = iProjectSubjectService.queryBySubjectId(enrollId, subjectId);
			if (projectSubject.getIsPay() == 1) {
				payState = "1";
			}
			page = iStudentInfoService.getPageStudent(param, enrollId, subjectId, name, auditState,payState);
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(page).build();
		} catch (Exception e) {
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_15010).data(e.toString()).build();
		}
	}

	@ApiOperation(value = "12.1.8  获取报名信息", notes = "14001:成功,15011未查找到报名项目信息")
	@ApiOperationSort(value = 8)
	@RequestMapping(value = "/queryProject", method = RequestMethod.GET)
	public Object queryProject(String projectId) {
		Project project = iProjectService.getById(projectId);
		if (project != null) {
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(project).build();
		} else {
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_15011).build();
		}
	}

	@ApiOperation(value = "12.1.9 查询项目列表", notes = "14001:成功")
	@ApiOperationSort(value = 9)
	@RequestMapping(value = "/queryProjectList", method = RequestMethod.GET)
	public Object queryProjectList() {
		List<Project> list = iProjectService.list();
		try {
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(list).build();
		} catch (Exception e) {
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_15016).data(list).build();
		}
	}

	@ApiOperation(value = "12.1.10 查询考生列表", notes = "14001:成功")
	@ApiOperationSort(value = 10)
	@RequestMapping(value = "/queryStudent", method = RequestMethod.GET)
	public Object queryStudent(StudentParam studentParam) throws Exception {
		List<String> permissionList = new ArrayList<String>();
		List<StudentVo> list = iStudentInfoService.queryStudentList(studentParam, permissionList,0L, 0L);
		try {
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(list).build();
		} catch (Exception e) {
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_15017).data(list).build();
		}
	}

	@ApiOperation(value = "12.1.6 考务删除考生时修改状态", notes = "14001:成功")
	@ApiOperationSort(value = 6)
	@RequestMapping(value = "/deleteCandidates", method = RequestMethod.GET)
	public Object deleteCandidates(String studentJson) {
		List<KwStudentVo> studentList = JSONObject.parseArray(studentJson, KwStudentVo.class);
		try {
			iStudentSubjectService.updateStudentFlag(studentList);
		} catch (Exception ex) {
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_14056).data("修改状态异常！").build();
		}

		return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data("修改状态成功！").build();
	}

	@ApiOperation(value = "12.1.7 统计考生科目数量", notes = "14001:成功")
	@ApiOperationSort(value = 7)
	@RequestMapping(value = "/statByExamId", method = RequestMethod.GET)
	public Object statByExamId(String examId) {
		try {
			List<StudentStatVo> list = iStudentSubjectService.statByExamId(examId);
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_SUCCESS).data(list).build();
		} catch (Exception ex) {
			return ApiResponse.builder().code(Constant.RESPONSE_CODE_15017).data("统计异常！").build();
		}
	}

}
